﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Web.Administration;
using UFC.Common;
using System.Diagnostics;

namespace IISAutoStart
{
    public class AutoStartApplicationPool
    {
        public void Excute() {
            //var appPoolName = "Web Application";
            //iis服务监视
            using (ServerManager sm = ServerManager.OpenRemote("localhost"))
            {
                //ApplicationPool appPool = sm.ApplicationPools.FirstOrDefault(x => x.Name == appPoolName);
                var appPoolList = sm.ApplicationPools.ToList();//获取iis所有应用程序池
                foreach (ApplicationPool appPool in appPoolList)
                {
                    if (appPool != null && appPool.State == ObjectState.Stopped)
                    {
                        if (appPool.Start() == ObjectState.Started)
                        {
                            LogHelp.WriteLog("成功启动应用池" + appPool.Name + "");
                        }
                        else
                        {
                            LogHelp.WriteLog("启动应用池" + appPool.Name + "失败");
                        }
                    }
                    //查找多个应用程序池的内存使用情况
                    // 创建性能计数器
                    PerformanceCounter counter = new PerformanceCounter("W3SVC_W3WP", "Process\\Private Bytes", "w3wp#" + appPool.Name);
                    // 获取内存使用情况
                    float memoryUsage = counter.NextValue();
                }
            }
        }

    }
}
